package net.sxnic.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import net.sxnic.comm.utils.CommUtils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MvcUtils {

	private static Logger logger = LoggerFactory.getLogger(MvcUtils.class);

	public static Object getEntityFromRequest(HttpServletRequest request,
			String entity, Object obj) throws Exception {
		String value = "";
		// String entity = "obj.";
		if (StringUtils.isBlank(entity)) {
			entity = obj.getClass().getSimpleName().toLowerCase();
		}
		Class<?> cls = obj.getClass();

		for (String key : request.getParameterMap().keySet()) {

			if (!key.startsWith(entity)) {
				continue;
			}

			Object valueObj = request.getParameterMap().get(key);

			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = request.getParameterValues(key);
				if (values != null) {
					for (int i = 0; i < values.length; i++) {
						value = values[i] + ",";
					}
					value = value.substring(0, value.length() - 1);
				}
			} else {
				value = String.valueOf(valueObj);
			}

			// 解析obj.xxx
			String fieldName = StringUtils.remove(key, entity);
			//
			fieldName = fieldName.substring(0, 1).toUpperCase()
					+ fieldName.substring(1);
			String setMethodName = "set" + fieldName;
			String getMethodName = "get" + fieldName;

			Method setMethod;
			Method getMethod;

			try {
				getMethod = cls.getMethod(getMethodName, new Class[] {});
			} catch (Exception e1) {
				getMethod = cls.getSuperclass().getMethod(getMethodName,
						new Class[] {});
			}

			Class<?> fieldType = getMethod.getReturnType();

			try {
				setMethod = cls.getMethod(setMethodName, fieldType);
			} catch (Exception e) {
				setMethod = cls.getSuperclass().getMethod(setMethodName,
						fieldType);
			}
			if ("String".equals(fieldType.getSimpleName())) {
				// 同名参数
				setMethod.invoke(obj, value);

			} else if ("Boolean".equals(fieldType.getSimpleName())
					|| "boolean".equals(fieldType.getSimpleName())) {
				getMethodName = "is" + fieldName;
				if ("true".equals(value)) {
					setMethod.invoke(obj, true);
				} else {
					setMethod.invoke(obj, false);
				}
			} else if ("Date".equals(fieldType.getSimpleName())) {
				setMethod.invoke(obj,
						CommUtils.FormatStringToDate(value, "yyyy-MM-dd"));
			} else if ("Integer".equals(fieldType.getSimpleName())) {
				setMethod.invoke(obj, Integer.parseInt(value));
			} else if ("int".equals(fieldType.getSimpleName())) {
				setMethod.invoke(obj, Integer.parseInt(value));
			} else if ("BigDecimal".equals(fieldType.getSimpleName())) {
				setMethod.invoke(obj,
						BigDecimal.valueOf(Double.parseDouble(value)));
			} else if ("Double".equals(fieldType.getSimpleName())) {
				setMethod.invoke(obj, Double.parseDouble(value));
			} else {
				logger.error("=======other type:" + fieldType.getSimpleName());
			}

		}
		return obj;
	}

	public static String getMessage(HttpServletRequest request) {
		String msg = request.getParameter("msg");
		if (StringUtils.isNotBlank(msg)) {
			return PropertiesUtils.getText(msg);
		}else{
			Object obj = request.getAttribute("msg");
			if(obj==null){
				return "";
			}else{
				return PropertiesUtils.getText(msg.toString());
			}
		}
	}

}
